#include "swpactivitygraphicitemcontent.h"

#include "graphicitemcommon.h"

#include <ieditoritem.h>

#include <QtGui/QPainter>

using namespace core;
using namespace swp;

SwpActivityGraphicItemContent::SwpActivityGraphicItemContent(
    const QString& activityName, QGraphicsItem* parent) :
        QGraphicsSimpleTextItem(parent), IGraphicItemContent(),
        m_name(activityName),
        m_cropped(false)
{
    setText(activityName);
}

SwpActivityGraphicItemContent::~SwpActivityGraphicItemContent()
{

}

void SwpActivityGraphicItemContent::setActivityName(
    const QString& activityName)
{
    m_name = activityName;

    setText(activityName);

    const int width = boundingRect().width();

    const int pWidth = parentItem()->boundingRect().width() -
        (2 * PREFERRED_CONTENT_PADDING);

    if (width > pWidth)
    {
        QFontMetrics metrics(font());

        const QString elided = metrics.elidedText(
            m_name, Qt::ElideRight, pWidth);

        setText(elided);

        m_cropped = true;
    }
    else
    {
        m_cropped = false;
    }

    IGraphicEditorItem* parent = dynamic_cast<IGraphicEditorItem*>(
        parentItem());

    Q_ASSERT(parent != 0); // can't be null

    parent->handleContentRefresh();
}

QString SwpActivityGraphicItemContent::activityName() const
{
    return m_name;
}

QGraphicsItem* SwpActivityGraphicItemContent::graphicsItem() const
{
    return const_cast<SwpActivityGraphicItemContent*>(this);
}

void SwpActivityGraphicItemContent::drawThumbnail(
    QPainter& painter, const QRectF& boundingRect)
{
    // do nothing
}

QRectF SwpActivityGraphicItemContent::croppedBoundingRect() const
{
    const QRectF ret = QGraphicsSimpleTextItem::boundingRect();

    return ret;
}

void SwpActivityGraphicItemContent::recheckIfFits()
{
    const int pWidth = parentItem()->boundingRect().width() -
        (2 * PREFERRED_CONTENT_PADDING);

    QFontMetrics metrics(font());

    const int width = metrics.width(m_name);

    if (width <= pWidth)
    {
        setText(m_name);

        IGraphicEditorItem* parent = dynamic_cast<IGraphicEditorItem*>(
            parentItem());

        Q_ASSERT(parent != 0); // can't be null

        parent->handleContentRefresh();

        m_cropped = false;
    }
    else
    {
        m_cropped = true;
    }
}

void SwpActivityGraphicItemContent::setPalette(const Palette* const palette)
{

}

bool SwpActivityGraphicItemContent::doActionForProperty(
    const QString& property, const QVariantList& value,
    const core::SerializationEngine& helper)
{
    if (property == "name")
    {
        setActivityName(value.first().toString());
    }

    return true;
}

void SwpActivityGraphicItemContent::deserializationFinished(bool ok)
{
    // do nothing
}

QHash<QString, QVariantList> SwpActivityGraphicItemContent::serializationData(
    const core::SerializationEngine& helper) const
{
    QHash<QString, QVariantList> ret;

    QVariantList list;
    list.append(QVariant(m_name));

    ret["name"] = list;

    return ret;
}

QRectF SwpActivityGraphicItemContent::boundingRect() const
{
    QRectF ret = QGraphicsSimpleTextItem::boundingRect();

    if (m_cropped)
    {
        QFontMetrics metrics(font());

        const int width = metrics.width(m_name) +
            (2 * PREFERRED_CONTENT_PADDING);

        ret.setWidth(width);
    }
    else
    {
        ret.setWidth(ret.width() + (2 * PREFERRED_CONTENT_PADDING));
    }

    return ret;
}
